﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 会员卡管理系统
{
    public partial class VipManage : UserControl
    {
        public VipManage()
        {
            InitializeComponent();
        }
        Vipclass.tb_viphouseinfo tbVipinfo = new Vipclass.tb_viphouseinfo();
        Vipclass.tbVipHouseMenthod tbVipmenthod = new Vipclass.tbVipHouseMenthod();
        string str1;
        string str2;
        private void VipManage_Load(object sender, EventArgs e)
        {
            tbVipmenthod.tbVipfoFill(Viplv, imageList1);
        }

        private void 刷新ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            tbVipmenthod.tbVipfoFill(Viplv, imageList1);
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if(rbtid.Checked==true)
            {
                tbVipmenthod.tbVipfoFind(Viplv, imageList1, textBox1.Text.Trim(), 0);
            }
            else
            {
                if(rbtname.Checked==true)
                {
                    tbVipmenthod.tbVipfoFind(Viplv, imageList1, textBox1.Text.Trim(), 1);
                }
                else
                {
                    tbVipmenthod.tbVipfoFind(Viplv, imageList1, textBox1.Text.Trim(), 2);
                }
            }
        }

        private void Viplv_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (this.Viplv.SelectedItems.Count > 0)
                {
                    string strshow = Viplv.SelectedItems[0].Text;
                    tbVipinfo = tbVipmenthod.tbViptoshow(strshow);
                    labvipid.Text = tbVipinfo.Strvipid;
                    labvipname.Text = tbVipinfo.Strvipname;
                    labvipidcard.Text = tbVipinfo.Strvipidcard;
                    laballtime.Text = tbVipinfo.Intalltime.ToString();
                    tbVipmenthod.tbVipRecordfoFill(lvRecord, strshow);
                }
            }
            catch(Exception err)
            {
                MessageBox.Show(err.Message.ToString());
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            AddNewVip addvip = new AddNewVip();
            if(addvip.ShowDialog()==DialogResult.OK)
            {
                tbVipmenthod.tbVipfoFill(Viplv, imageList1);
            }
            else
            {
                tbVipmenthod.tbVipfoFill(Viplv, imageList1);
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.Viplv.SelectedItems.Count > 0)
                {
                    
                    string strshow = Viplv.SelectedItems[0].Text;
                    if (MessageBox.Show("是否删除会员" + strshow, "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                    {
                        if (tbVipmenthod.tbvipfodel(strshow) == 1)
                        {
                            MessageBox.Show("删除成功！", "提示");
                        }
                        else
                        {
                            MessageBox.Show("删除失败！", "错误");
                        }
                    }
                    else
                    {
                        return;
                    }
                }
                else
                {
                    MessageBox.Show("未选中删除项！", "提示");
                }
                tbVipmenthod.tbVipfoFill(Viplv, imageList1);
            }
            catch(Exception err)
            {
                MessageBox.Show(err.Message.ToString());
                tbVipmenthod.tbVipfoFill(Viplv, imageList1);
            }
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.Viplv.SelectedItems.Count > 0)
                {

                    string strshow = Viplv.SelectedItems[0].Text;
                    if (MessageBox.Show("是否删除会员" + strshow, "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                    {
                        if (tbVipmenthod.tbvipfodel(strshow) == 1)
                        {
                            MessageBox.Show("删除成功！", "提示");
                        }
                        else
                        {
                            MessageBox.Show("删除失败！", "错误");
                        }
                    }
                    else
                    {
                        return;
                    }
                }
                else
                {
                    MessageBox.Show("未选中删除项！", "提示");
                }
                tbVipmenthod.tbVipfoFill(Viplv, imageList1);
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message.ToString());
                tbVipmenthod.tbVipfoFill(Viplv, imageList1);
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                if (labvipid.Text != "")
                {
                    if (btnUpdate.Text == "修改信息")
                    {
                        MessageBox.Show("请在会员信息页面修改信息\r\n" + "修改完成按确认键！", "提示");
                        labvipname.ReadOnly = false;
                        labvipidcard.ReadOnly = false;
                        btnUpdate.Text = "确认";
                        str1 = labvipname.Text;
                        str2 = labvipidcard.Text;
                    }
                    else
                    {
                        tbVipinfo.Strvipid = labvipid.Text.Trim();
                        tbVipinfo.Strvipname = labvipname.Text.Trim();
                        tbVipinfo.Strvipidcard = labvipidcard.Text.Trim();
                        if (MessageBox.Show("是否修改？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                        {
                            if (tbVipmenthod.tbvipfoupdate(tbVipinfo) == 1)
                            {
                                MessageBox.Show("修改成功！","提示");

                            }
                            else
                            {
                                MessageBox.Show("修改失败！", "错误");
                            }
                            labvipname.ReadOnly = true;
                            labvipidcard.ReadOnly = true;
                            btnUpdate.Text = "修改信息";
                        }
                        else
                        {
                            labvipname.Text = str1;
                            labvipidcard.Text = str2;
                            labvipname.ReadOnly = true;
                            labvipidcard.ReadOnly = true;
                            btnUpdate.Text = "修改信息";
                        }
                    }
                }
                else
                {
                    MessageBox.Show("未选中修改项！", "提示");
                }
                tbVipmenthod.tbVipfoFill(Viplv, imageList1);
            }
            catch(Exception err)
            {
                MessageBox.Show(err.Message.ToString());
                tbVipmenthod.tbVipfoFill(Viplv, imageList1);
            }
        }
    }
}
